/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary
{
    public class MiscHelpers
    {
        static public System.Xml.XmlElement CreateElement(System.Xml.XmlDocument i_XmlDocument, System.Xml.XmlElement i_ParentXmlElement, string i_ElementName, string i_NamespaceURI, string i_NamespacePrefix, System.Xml.XmlNamespaceManager i_XmlNamespaceManager, bool i_AlwaysCreateNewElement)
        {
            System.Xml.XmlElement XmlElement = null;

            string ElementName = i_ElementName;
            if (!System.String.IsNullOrEmpty(i_NamespacePrefix))
                ElementName = i_NamespacePrefix + ":" + i_ElementName;

            bool CreateNewElement = i_AlwaysCreateNewElement;

            if (!i_AlwaysCreateNewElement)
            {
                //  See if node already exists
                XmlElement = (System.Xml.XmlElement)i_ParentXmlElement.SelectSingleNode(ElementName, i_XmlNamespaceManager);

                CreateNewElement = (XmlElement == null);
            }

            //  Determine if we should create new node
            if (CreateNewElement)
            {
                //  Create and append new node
                XmlElement = i_XmlDocument.CreateElement(ElementName, i_NamespaceURI);
                i_ParentXmlElement.AppendChild(XmlElement);
            }

            return XmlElement;
        }

        static public System.Xml.XmlElement CreateElementHierarchy(System.Xml.XmlDocument i_XmlDocument, System.Xml.XmlElement i_ParentXmlElement, string[] i_ElementNames, string[] i_NamespaceURIs, string[] i_NamespacePrefixes, System.Xml.XmlNamespaceManager i_XmlNamespaceManager)
        {
            return Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.MiscHelpers.CreateElementHierarchy
                (
                i_XmlDocument,
                i_ParentXmlElement,
                i_ElementNames,
                i_NamespaceURIs,
                i_NamespacePrefixes,
                i_XmlNamespaceManager,
                false
                );
        }

        static public System.Xml.XmlElement CreateElementHierarchy(System.Xml.XmlDocument i_XmlDocument, System.Xml.XmlElement i_ParentXmlElement, string[] i_ElementNames, string[] i_NamespaceURIs, string[] i_NamespacePrefixes, System.Xml.XmlNamespaceManager i_XmlNamespaceManager, bool i_AlwaysCreateLastElement)
        {
            System.Xml.XmlElement XmlElement = null;
            System.Xml.XmlElement ParentXmlElement = i_ParentXmlElement;

            for (int Index = 0; Index < i_ElementNames.Length; ++Index)
            {
                string ElementName = i_ElementNames[Index];
                string NamespaceURI = i_NamespaceURIs[Index];
                string NamespacePrefix = i_NamespacePrefixes[Index];

                bool ForceElementCreation = (i_AlwaysCreateLastElement && (Index == i_ElementNames.Length - 1));

                XmlElement = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.MiscHelpers.CreateElement
                    (
                    i_XmlDocument,
                    ParentXmlElement,
                    ElementName,
                    NamespaceURI,
                    NamespacePrefix,
                    i_XmlNamespaceManager,
                    ForceElementCreation
                    );

                ParentXmlElement = XmlElement;
            }

            return XmlElement;
        }

        static public string[] GetFieldIDsForExpression(string i_ExpressionString)
        {
            System.Collections.Generic.List<string> FieldIds = new System.Collections.Generic.List<string>();

            string[] LeftExpressionParts = i_ExpressionString.Split(new char[] { '{' });
            for (int Index = 0; Index < LeftExpressionParts.Length; ++Index)
            {
                int RightParenthesisIndex = LeftExpressionParts[Index].LastIndexOf("}");
                if (RightParenthesisIndex == -1)
                    continue;

                string FieldID = LeftExpressionParts[Index].Substring(0, RightParenthesisIndex);
                FieldIds.Add(FieldID);
            }

            return FieldIds.ToArray();
        }

    }
}
